<?php
/*
 * program:	均源
 * author:	雨中漫步 & 上善若水 & qhxin
 * email:	qhxin@qhxin.com
 * date:	2014/11/11
 * description:	又是一年双十一，不能剁手，就写点东西吧~~~~~~
 */

defined('IN_SCHOOL') or die();

/*{TranslateTagBegin}*/
class html {

	/*
	获取页数
	*/
	public static function page($key = 'page') {
		return max(1, intval(core::gpc($key, 'R')));
	}
	
	/*
	绘制分页
	例：html::pages('index.php?a=b', 100, 1, 20);
	*/
	public static function pages($url, $totalnum, $page, $pagesize = 20, $pagename = 'page',$shownum = 2) {
		$urladd = '';
		if(strpos($url, '.htm') !== FALSE) {
			list($url, $urladd) = explode('.htm', $url);
			$urladd = '.htm'.$urladd;
			$rewritepage = "-$pagename-";
		} else {
			$url .= strpos($url, '?') === FALSE ? '?' : '&';
			$rewritepage = "$pagename=";
		}

		$totalpage = ceil($totalnum / $pagesize);
		if($totalpage < 2) return '';
		$page = min($totalpage, $page);
		
		$start = max(1, $page - $shownum);
		$end = min($totalpage, $page + $shownum);
		
		$right = $page + $shownum - $totalpage;
		$right > 0 && $start = max(1, $start -= $right);
		$left = $page - $shownum;
		$left < 0 && $end = min($totalpage, $end -= $left);
		
		$s = '';
		$page != 1 && $s .= '<a href="'.$url.$rewritepage.($page - 1).$urladd.'">上页</a>';
		if($start > 1) $s .= '<a href="'.$url.$rewritepage.'1'.$urladd.'">1 '.($start > 2 ? '... ' : '').'</a>';
		for($i=$start; $i<=$end; $i++) {
			if($i == $page) {
				$s .= '<a href="'.$url.$rewritepage.$i.$urladd.'" class="checked">'.$i.'</a>';// checked
			} else {
				$s .= '<a href="'.$url.$rewritepage.$i.$urladd.'">'.$i.'</a>';
			}
		}
		if($end != $totalpage) $s .= '<a href="'.$url.$rewritepage.$totalpage.$urladd.'">'.($totalpage - $end > 1 ? '... ' : '').$totalpage.'</a>';
		$page != $totalpage && $s .= '<a href="'.$url.$rewritepage.($page + 1).$urladd.'">下页</a>';
		return $s;
	}
	
	/*
	保存cookie
	*/
	public static function setcookie($key, $value, $time = 0, $path = '', $domain = '', $httponly = FALSE) {
		$_COOKIE[$key] = $value;
		if($value != NULL) {
			if(version_compare(PHP_VERSION, '5.2.0') >= 0) {
				setcookie($key, $value, $time, $path, $domain, FALSE, $httponly);
			} else {
				setcookie($key, $value, $time, $path, $domain, FALSE);
			}
		} else {
			if(version_compare(PHP_VERSION, '5.2.0') >= 0) {
				setcookie($key, '', $time, $path, $domain, FALSE, $httponly);
			} else {
				setcookie($key, '', $time, $path, $domain, FALSE);
			}
		}
	}
	
	/*
	生成表单校验
	*/
	public static function form_hash($auth_key) {
		return substr(md5(substr($_SERVER['time'], 0, -5).$auth_key), 16);
	}
	
	/*
	校验 formhash
	*/
	public static function form_submit($auth_key) {
		$hash = core::gpc('FORM_HASH', 'R');
		return $hash == self::form_hash($auth_key);
	}
	
	/*
	转换空白字符, $onlytab 仅仅转换 \t
	*/
	public static function html_space($s, $onlytab = FALSE) {
		if(!$onlytab) {
			$s = str_replace('  ', ' &nbsp;', $s);
			$s = str_replace('  ', ' &nbsp;', $s);
			$s = str_replace('  ', ' &nbsp;', $s);
			$s = str_replace("\r\n", "\n", $s);
			$s = str_replace("\n", "<br />", $s);
		}
		$s = str_replace("\t", '&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ', $s);
		return $s;
	}
	
	/*
	重定向
	*/
	public static function html_locate($url = ''){
		echo 'exit';
		header("Location: $url");
		exit;
	}
}
/*{TranslateTagEnd}*/

?>